const ejs = require("ejs");
const url = require("url");

let app = {
    home: function (req, res) {
        let msg = "这是后台的数据";
        let list = [
            {name: "张三", age: 20},
            {name: "李四", age: 30},
            {name: "王五", age: 40}
        ];
        let h2 = "<h2>这是html的h2标签</h2>";
        ejs.renderFile("./views/index.html", {msg: msg, list: list, h2: h2}, function (err, data) {
            res.end(data);
        });
    },
    login: function (req, res) {
        ejs.renderFile("./views/login.html", {}, function (err, data) {
            res.end(data);
        });
    },
    dologin: function (req, res) {
        let method = req.method.toLocaleLowerCase();
        if (method === "get") {
            let getData = url.parse(req.url).query;
            res.end(decodeURI(getData));
        } else {
            let postStr = "";
            req.on("data", function (postData) {
                postStr += postData;
            });
            req.once("end", function () {
                postStr = decodeURI(postStr);
                res.end("<script>alert('登陆成功\\n" + postStr + "');history.back();</script>");
            });
        }
    }
}

module.exports = app;